
#include "vxfont.h"

/*
 *
 *
 * VxFont
 *
 *
 */

VxFont::VxFont(QFont * font) :
		_font(font)
{
	_metrics = new QFontMetrics(*_font);
}

QPainterPath * VxFont::getPath(QChar ch)
{
	QPainterPath * path = new QPainterPath();
	path->addText(0,0, *_font, QString(ch));

	return path;
}


/*
 *
 *
 * VxSymbolsGenerator
 *
 *
 */

VxSymbolsGenerator::VxSymbolsGenerator()
{
	_count = 255 - 32;
}



QChar VxSymbolsGenerator::charAt(int cr) const
{
	int ichr;
	if (cr < 0){
		ichr = _count - abs(cr)%_count;
	} else {
		ichr = cr % _count;
	}

	return QChar(32 + ichr);
}


/*
 *
 *
 * VxSymbols
 *
 *
 */

VxSymbols::VxSymbols(VxFont * font, VxSymbolsGenerator * generator):
		_font(font), _generator(generator)
{
	_pathes = new QHash<int, QPainterPath *>();
}

QSize VxSymbols::getMaxSize(int charIx0, int length)
{
	int ci0 = (charIx0 < 0)? 0 : charIx0;
	int ci1 = (length < 1)? (_generator->count() - 1) : ci0 + length ;
	ci1 = (ci1 < _generator->count()) ? ci1: _generator->count()-1;
	int maxw = 0;
	int maxh = 0;
	for (int chix=ci0; chix <= ci1; chix++){
		if (!_pathes->contains(chix)){
			_pathes->insert(chix, _font->getPath(_generator->charAt(chix)));
		}
		QPainterPath * path = _pathes->value(chix);
		int w = path->boundingRect().width();
		int h = path->boundingRect().height();
		maxw = (w > maxw) ? w : maxw;
		maxh = (h > maxh) ? h : maxh;
	}
	return QSize(maxw, maxh);
}


int VxSymbols::descent()
{
	return _font->metrics()->descent();
}



